package com.ruoyi.bom.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.bom.domain.TqlMaterialTransUnit;
import com.ruoyi.bom.domain.TqlStapPrice;
import com.ruoyi.bom.dto.StapAndPriceAndUnitTransfer;
import com.ruoyi.bom.service.ITqlMaterialTransUnitService;
import com.ruoyi.bom.service.ITqlStapPriceService;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RemoteKingdeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.bom.domain.TqlStapBase;
import com.ruoyi.bom.service.ITqlStapBaseService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 物料基础Controller
 * 
 * @author ruoyi
 * @date 2023-12-31
 */
@RestController
@Api(tags = "物料管理")
@RequestMapping("/base")
public class TqlStapBaseController extends BaseController {
    @Autowired
    private ITqlMaterialTransUnitService tqlMaterialTransUnitService;
    @Autowired
    private ITqlStapBaseService tqlStapBaseService;

    @Autowired
    private ITqlStapPriceService tqlStapPriceService;
    @Autowired
    private RemoteKingdeeService remoteKingdeeService;

//    @RequiresPermissions("bom:base:all")
    @ApiOperation("物料分页")
    @GetMapping("/list")
    public TableDataInfo selectStapBaseList(TqlStapBase tqlStapBase) {
        startPage();
        List<TqlStapBase> list = tqlStapBaseService.selectStapBaseList(tqlStapBase);
        return getDataTable(list);
    }



    @ApiOperation("物料选中")
    @GetMapping("/selectedStapByIdForBom")
    public AjaxResult selectedStapByIdForBom(@RequestParam("id") String id,@RequestParam("deptCode") String deptCode) {

        StapAndPriceAndUnitTransfer result=new StapAndPriceAndUnitTransfer();
        TqlStapBase base=tqlStapBaseService.selectTqlStapBaseById(Integer.parseInt(id));

        TqlStapPrice search=new TqlStapPrice();
        String stapCode=base.getStapCode();
        search.setStapCode(stapCode);
        search.setDeptCode(deptCode);
        List<TqlStapPrice> list =tqlStapPriceService.selectTqlStapPriceList(search);
        BigDecimal price=null;
        if(!CollectionUtils.isEmpty(list))
        {
            BigDecimal BdZero=BigDecimal.ZERO;
            BigDecimal wp=list.get(0).getWeightedPrice();
            BigDecimal wp1=null==wp?BdZero:wp;
            BigDecimal ip=list.get(0).getInitPrice();
            BigDecimal ip1=null==ip?BdZero:ip;
            price=wp1.equals(BdZero)?ip1:wp1;
            base.setPrice(price);
        }
        result.setBase(base);
// base
        int state=0;
        String[] unitArr=new String[]{"千克","克","KG","KG1"};
        String baseUnit=base.getStapBaseUnit();

        boolean isInUnitArr = Arrays.asList(unitArr).contains(baseUnit);
        //----------------------------------------
        //第一种情况 是标准单位
        if(isInUnitArr)
        {
            base.setStapAuxUnit(baseUnit);
            state=1;
        }
        else{
            String auxUnit=base.getStapAuxUnit();
            if(!StringUtils.isEmpty(auxUnit))//有辅助段位
            {

                TqlMaterialTransUnit unit= tqlMaterialTransUnitService.selectTqlMaterialTransUnitByFMaterialIdfnumber(stapCode);
                if(null!=unit)
                {
                    state=2;
                    result.setMt(unit);
                }
                else
                {
                    if(baseUnit.equals(auxUnit))
                    {
                        state=5;
                    }
                    else {
                        state=3;
                    }

                }



            }else{
                state=4;
            }
        }
        //----------------------------------------
        result.setFlag(state);
        return AjaxResult.success(result);
    }



    @RequiresPermissions("bom:dishbom:add")
    @ApiOperation("物料选中")
    @GetMapping("/selectedStapById")
    public AjaxResult selectedStapById(@RequestParam("id") String id,@RequestParam("deptCode") String deptCode) {


        TqlStapBase base=tqlStapBaseService.selectTqlStapBaseById(Integer.parseInt(id));

        TqlStapPrice search=new TqlStapPrice();
        String stapCode=base.getStapCode();
        search.setStapCode(stapCode);
        search.setDeptCode(deptCode);
        List<TqlStapPrice> list =tqlStapPriceService.selectTqlStapPriceList(search);
        BigDecimal price=null;
        if(!CollectionUtils.isEmpty(list))
        {
            BigDecimal BdZero=BigDecimal.ZERO;
            BigDecimal wp=list.get(0).getWeightedPrice();
            BigDecimal wp1=null==wp?BdZero:wp;
            BigDecimal ip=list.get(0).getInitPrice();
            BigDecimal ip1=null==ip?BdZero:ip;
            price=wp1.equals(BdZero)?ip1:wp1;
            base.setPrice(price);
        }


        return AjaxResult.success(base);
    }





    @RequiresPermissions("bom:base:all")
    @ApiOperation("导出物料基础")
    @Log(title = "导出物料基础", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TqlStapBase tqlStapBase) {
        List<TqlStapBase> list = tqlStapBaseService.selectTqlStapBaseList(tqlStapBase);
        ExcelUtil<TqlStapBase> util = new ExcelUtil<TqlStapBase>(TqlStapBase.class);
        util.exportExcel(response, list, "物料基础数据");
    }

    @ApiOperation("获取物料基础详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        tqlStapBaseService.selectTqlStapBaseById(id);

        return success();
    }

    @ApiOperation("新增物料基础")
    @RequiresPermissions("bom:base:all")
    @Log(title = "物料基础", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TqlStapBase tqlStapBase) {
        return toAjax(tqlStapBaseService.insertTqlStapBase(tqlStapBase));
    }

    @ApiOperation("修改物料基础")
    @RequiresPermissions("bom:base:all")
    @Log(title = "物料基础", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TqlStapBase tqlStapBase)
    {
        return toAjax(tqlStapBaseService.updateTqlStapBase(tqlStapBase));
    }

    @ApiOperation("删除物料基础")
    @RequiresPermissions("bom:base:all")
    @Log(title = "物料基础", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(tqlStapBaseService.deleteTqlStapBaseByIds(ids));
    }

    @ApiOperation("物料及价格信息")
    @GetMapping("/stapAndPrice")
    public TableDataInfo selectStapAndPriceList(TqlStapBase tqlStapBase){

        //获取当前登录人shopId
        String deptCode = SecurityUtils.getLoginUser().getSysUser().getDeptCode();
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            tqlStapBase.setDeptCode(deptCode);
        }



        startPage();
        List<TqlStapBase> list = tqlStapBaseService.selectStapAndPriceList(tqlStapBase);
        return getDataTable(list);
    }

    @ApiOperation("更新物料")
    @RequiresPermissions("bom:base:getRemoteStap")
    @GetMapping("/getRemoteStap")
    public AjaxResult getRemoteStap(@RequestParam("stapCode") String stapCode)
    {
        remoteKingdeeService.saveStapByStapcode(stapCode);
        return success();
    }


}
